package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SalaryBasicAddDTO;
import org.jsola.hr.dto.SalaryBasicUpdateDTO;
import org.jsola.hr.query.SalaryBasicQuery;
import org.jsola.hr.query.SalaryChangesQuery;
import org.jsola.hr.service.ISalaryBasicService;
import org.jsola.hr.vo.SalaryBasicListVO;
import org.jsola.hr.vo.SalaryBasicVO;
import org.jsola.hr.vo.SalaryChangesVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 员工薪资表
 *
 * @author zhr
 */
@RestController("hrSalaryBasicController")
@RequestMapping(HrConstants.API_PATH + "/v1/salaryBasics")
public class SalaryBasicController implements ISalaryBasicControllerAPI {

    @Autowired
    private ISalaryBasicService salaryBasicService;

    @Override
    @PostMapping
    public Result<SalaryBasicVO> save(@Valid @RequestBody SalaryBasicAddDTO salaryBasicAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工薪资表权限校验

        return Result.success(
                salaryBasicService.save(salaryBasicAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SalaryBasicUpdateDTO salaryBasicUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工薪资表权限校验

        int count = salaryBasicService.update(salaryBasicUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{salaryBasicId}")
    public Result<Integer> delete(@PathVariable String salaryBasicId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工薪资表权限校验

        int count = salaryBasicService.deleteByIds(tokenUser, salaryBasicId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] salaryBasicIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工薪资表权限校验

        int count = salaryBasicService.deleteByIds(tokenUser, salaryBasicIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{salaryBasicId}")
    public Result<SalaryBasicVO> findById(@PathVariable String salaryBasicId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工薪资表权限校验

        return Result.success(
            salaryBasicService.selectById(salaryBasicId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SalaryBasicListVO>> listAll(@Valid SalaryBasicQuery salaryBasicQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工薪资表权限校验

        return Result.success(
            salaryBasicService.select(salaryBasicQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SalaryBasicListVO>> page(@Valid SalaryBasicQuery salaryBasicQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            salaryBasicService.selectPage(salaryBasicQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping(value = "/saveSalaryBasic")
    public Result<SalaryBasicVO> saveSalaryBasic(@Valid @RequestBody SalaryBasicAddDTO salaryBasicAddDTO){
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工资基础信息权限校验

        return Result.success(
                salaryBasicService.saveSalaryBasic(salaryBasicAddDTO, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectPageEmpSalary")
    public Result<Page<SalaryBasicVO>> selectPageEmpSalary(@Valid SalaryBasicQuery salaryBasicAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工资基础信息权限校验

        return Result.success(
                salaryBasicService.selectPageEmpSalary(salaryBasicAddDTO, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/selectEmpSalary")
    public Result<SalaryBasicVO> selectEmpSalary(String id) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工资基础信息权限校验

        return Result.success(
                salaryBasicService.selectEmpSalary(id, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping(value = "/insertEmpSalary")
    public Result<Integer> insertEmpSalary(@Valid @RequestBody SalaryBasicAddDTO salaryBasicAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工资基础信息权限校验

        return Result.success(
                salaryBasicService.insertEmpSalary(salaryBasicAddDTO, tokenUser)
        );
    }

    @Override
    @PutMapping(value = "/updateEmpSalary")
    public Result<Integer> updateEmpSalary(@Valid @RequestBody SalaryBasicAddDTO salaryBasicAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工资基础信息权限校验

        return Result.success(
                salaryBasicService.updateEmpSalary(salaryBasicAddDTO, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectPageChanges")
    public Result<Page<SalaryChangesVO>> selectPageChanges(@Valid SalaryChangesQuery salaryChangesQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工资基础信息权限校验

        return Result.success(
                salaryBasicService.selectPageChanges(salaryChangesQuery, tokenUser.getSiteId())
        );
    }
}

